<template>
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    :before-close="clearForm"
    width="70%"
  >
    <!-- 弹窗内容 -->
    <span>
      <el-form
        :model="goodsForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="类目选择" prop="category">
          <el-button type="primary" @click="innerVisible = true"
            >类目选择</el-button
          >
          <span class="category">{{ goodsForm.category }}</span>
        </el-form-item>
        <el-form-item label="商品名称" prop="title">
          <el-input v-model="goodsForm.title"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="goodsForm.price"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="num">
          <el-input v-model="goodsForm.num"></el-input>
        </el-form-item>
        <el-form-item label="添加时间" prop="time">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="goodsForm.time"
            style="width: 150px"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="商品卖点" prop="sellPoint">
          <el-input v-model="goodsForm.sellPoint"></el-input>
        </el-form-item>
        <el-form-item label="商品图片" prop="image">
          <el-button type="primary" @click="innerVisibleImg = true"
            >上传</el-button
          >
          <img
            :src="goodsForm.image"
            height="200px"
            style="margin-left: 20px"
            alt=""
          />
        </el-form-item>
        <el-form-item label="商品描述" prop="descs">
          <WangEitor ref="myEditor" @sendTxt="sendTxt" />
        </el-form-item>
        <!-- 规格参数配置 -->
        <el-form-item label="规格参数配置" v-show="isShow">
          <el-form
            ref="dynamicValidateForm"
            label-width="120px"
            class="demo-dynamic"
          >
            <el-form-item
              v-for="(item, index) in groups"
              :label="item.title"
              :key="index"
              :prop="item.value"
            >
              <el-input v-model="item.value"></el-input>
              <!-- 内层的表单项 -->
              <el-form-item
                v-for="(ele, i) in item.children"
                :label="ele.title"
                :key="i"
                :prop="ele.value"
              >
                <el-input v-model="ele.value"></el-input>
              </el-form-item>
            </el-form-item>
          </el-form>
        </el-form-item>
      </el-form>
    </span>
    <!-- 弹窗底部 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="clearForm">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
    <!-- 内弹框----类目选择 -->
    <el-dialog
      width="45%"
      title="类目选择"
      :visible="innerVisible"
      append-to-body
      :show-close="false"
    >
      <TreeGoods @sendTreeData="sendTreeData" />

      <span slot="footer" class="dialog-footer">
        <el-button @click="innerVisible = false">取 消</el-button>
        <el-button type="primary" @click="showTreeData">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 内弹框----上传图片 -->
    <el-dialog
      width="45%"
      title="上传图片"
      :visible="innerVisibleImg"
      append-to-body
      :show-close="false"
    >
      <SendImg @sendImg="sendImg" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="innerVisibleImg = false">取 消</el-button>
        <el-button type="primary" @click="showImg">确 定</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>

<script>
import WangEitor from "./WangEitor.vue";
import SendImg from "./SendImg.vue";
import TreeGoods from "./TreeGoods.vue";
import { nextTick } from "@vue/runtime-core";
export default {
  props: {
    title: {
      type: String,
      default: "商品添加",
    },
    rowData: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },
  components: { TreeGoods, SendImg, WangEitor },
  data() {
    return {
      dialogVisible: false,
      innerVisible: false,
      innerVisibleImg: false,
      treeDate: {},
      imgData: "",
      goodsForm: {
        id: "",
        title: "", //商品名称
        price: "", //商品价格
        num: "", //商品数量
        sellPoint: "", //商品卖点
        image: "", //商品图片
        descs: "", //商品描述
        cid: "",
        category: "", //类目规格
        time: "",
      },
      groups:[],
      rules: {
        title: [
          {
            required: true,
            message: "请输入商品名称",
            trigger: "blur",
          },
          { min: 1, max: 20, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        price: [
          {
            required: true,
            message: "请输入商品价格",
            trigger: "blur",
          },
        ],
        num: [
          {
            required: true,
            message: "请输入商品数量",
            trigger: "blur",
          },
          { min: 1, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
      isShow: false, //是否显示规格参数配置
    };
  },
  watch: {
    rowData(val) {
      console.log(val);
      this.rowData = val;
      this.goodsForm = val;
      // 富文本编辑器内容
      this.$nextTick(() => {
        this.$refs.myEditor.editor.txt.html(val.descs);
      });
       //是否显示规格配置参数----------------------
      if(val.paramsInfo){//显示规格参数
        this.isShow = true;
        this.groups = JSON.parse(val.paramsInfo)
      }
    },
  },
  methods: {
    // 获取treeData数据
    sendTreeData(val) {
      this.treeDate = val;
    },
    //显示treeData数据
    showTreeData() {
      this.innerVisible = false;
      this.goodsForm.category = this.treeDate.name;
      this.goodsForm.cid = this.treeDate.cid;
      // 显示规格参数配置
     this.$api
        .getCategoryData({
          cid: this.treeDate.cid,
        })
        .then((res) => {
          console.log("显示规格参数--- 获取-", res.data);
          if (res.data.status === 200) {
            //有类目规格配置参数-------------
            this.isShow = true;
            //存储规格参数
            let result = res.data.result[res.data.result.length - 1];
            console.log(result.paramData);
            this.groups = JSON.parse(result.paramData);
          } else {
            this.isShow = false;
          }
        });
    },
    // 显示图片路径
    sendImg(val) {
      this.imgData = val;
    },
    // 展示图片
    showImg() {
      this.innerVisibleImg = false;
      this.goodsForm.image = this.imgData;
    },
    // 获取富文本编译器的内容
    sendTxt(val) {
      this.goodsForm.descs = val;
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let {
            title,
            cid,
            category,
            sellPoint,
            price,
            num,
            descs,
            image,
            id,
          } = this.goodsForm;
          // 判断弹框类型 如果是添加执行AddGood否则执行editGoods
          if (this.title === "商品添加") {
            this.$api
              .AddGood({
                id,
                title,
                cid,
                category,
                sellPoint,
                price,
                num,
                descs,
                image,
                paramsInfo:JSON.stringify(this.groups)
              })
              .then((res) => {
                if (res.data.status === 200) {
                  //成功
                  this.dialogVisible = false; //关闭表单弹框
                  //更新父组件列表数据
                  this.$parent.getGoodPage(1);
                  this.$message({
                    message: "恭喜你，添加商品成功",
                    type: "success",
                  });
                  //清空表单数据，方便二次填写
                  this.clearForm();
                } else {
                  this.$message.error("添加失败请检查信息");
                }
              });
          } else {
            this.$api
              .editGoods({
                id,
                title,
                cid,
                category,
                sellPoint,
                price,
                num,
                descs,
                image,
                paramsInfo:JSON.stringify(this.groups)
              })
              .then((res) => {
                if (res.data.status === 200) {
                  this.dialogVisible = false; //关闭表单弹框
                  this.$parent.getGoodPage(1);
                  this.$message({
                    message: "恭喜你，修改商品成功",
                    type: "success",
                  });
                } else {
                  this.$message.error("修改失败请检查信息");
                }
              });
          }
        } else {
          console.log("校验未通过，请重新输入");
          // console.log(this.$parent.getGoodPage);
          return false;
        }
      });
    },
    clearForm() {
      this.$refs.ruleForm.resetFields();
      this.goodsForm = {
        title: "", //商品名称
        price: "", //商品价格
        num: "", //商品数量
        sellPoint: "", //商品卖点
        image: "", //商品图片
        descs: "", //商品描述
        cid: "",
        category: "", //类目规格
        time: "",
      };
      this.dialogVisible = false;//关闭表单弹框
      this.groups = [];
      this.isShow = false;
    },
  },
};
</script>

<style lang="less" scoped>
.category {
  margin-left: 10px;
  font-size: 16px;
}
</style>
